package com.skyline.spot.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.skyline.base.type.Activity;
import com.skyline.base.type.Authority;
import com.skyline.base.type.IdolType;
import com.skyline.base.type.SpotType;
import com.skyline.common.bean.Page;
import com.skyline.spot.dao.SpotDao;
import com.skyline.spot.model.Spot;
import com.skyline.spot.service.SpotService;
import com.skyline.user.dao.FanDao;
import com.skyline.user.model.User;
import com.skyline.wo.dao.AlbumDao;

@Service("spotService")
public class SpotServiceImpl implements SpotService {

	@Autowired
	private SpotDao spotDao;
	@Autowired
	private FanDao fanDao;
	@Autowired
	private AlbumDao albumDao;

	@Value("${album.cover.defaultCover}")
	private String cover;
	@Value("${album.cover.defaultExt}")
	private String ext;

	@Override
	public Long discoverySpot(User user, Spot spot) {
		// Object obj =new Object();
		long spotId = spotDao.insertSpot(spot.getName(), spot.getCategory(), user.getId(), user
				.getNickname(), user.getPortrait(), user.getEmail(),
				spot.getCountry().split("-")[0], spot.getProvince().split("-")[0], spot.getCity()
						.split("-")[0], spot.getCity().split("-")[1], spot.getDetailAddress(), spot
						.getLatitude(), spot.getLongitude(), spot.getIntroduction());

		if (spotId != 0) {
			albumDao.insertAlbumReturnId(spotId, spot.getName(), "default", "头像相册", "default", "",
					Authority.ADMIN, Activity.UNDELETABLE, cover, ext);
			albumDao.insertAlbumReturnId(spotId, spot.getName(), "default", "默认相册", "default", "",
					Authority.PUBLIC, Activity.UNDELETABLE, cover, ext);
			fanDao.insertIdolAndFans(spotId, spot.getName(), "default", user.getId(),
					user.getNickname(), user.getPortrait(), IdolType.SPOT, false);

		}
		return spotId;

	}

	@Override
	public void updateSpotInfo(Spot spot) {
		spotDao.updateSpot(spot.getId(), spot.getName(), spot.getCategory(), spot.getCountry()
				.split("-")[0], spot.getProvince().split("-")[0], spot.getCity().split("-")[0],
				spot.getCity().split("-")[1], spot.getDetailAddress(), spot.getLatitude(), spot
						.getLongitude());
	}

	@Override
	public void updateSpotIntroduction(Long spotId, String content) {
		spotDao.updateSpotIntroduction(spotId, content);

	}

	@Override
	public void updateSpotPortrait(Long spotId, String portrait) {
		spotDao.updateSpotPortrait(spotId, portrait);

	}

	@Override
	public void addVisitNum(Long spotId) {
		spotDao.updateSpotVistNum(spotId);

	}

	@Override
	public void changeAdministrator(Long spotId, User oldAdministrator, User newAdministrator) {
		spotDao.updateSpotAdministrator(spotId, newAdministrator.getId(),
				newAdministrator.getNickname(), newAdministrator.getEmail(),
				newAdministrator.getPortrait());

	}

	@Override
	public void deleteSpot(Long spotId) {
		spotDao.deleteSpot(spotId);

	}

	@Override
	public boolean isExist(String spotName) {
		List<Spot> list = spotDao.querySpotsByName(spotName, SpotType.VIEWPOINT, Activity.NORMAL);
		if (list == null || list.isEmpty()) {
			return false;
		}
		return true;

	}

	@Override
	public Spot votes(Long spotId, Integer star) {
		// TODO Auto-generated method stub

		return spotDao.updateSpotStar(spotId, star);

	}

	@Override
	public Spot view(Long spotId) {
		return spotDao.querySpotById(spotId, Activity.NORMAL);
	}

	@Override
	public Spot getSpot(Long spotId) {
		return spotDao.querySpotById(spotId, Activity.NORMAL);
	}

	@Override
	public List<Spot> findByName(String name, SpotType type, Activity activity) {
		return spotDao.querySpotsByName(name, type, activity);
	}

	@Override
	public List<Spot> findAll(SpotType type, Page page, Activity activity) {
		return spotDao.querySpotsAll(type, page, activity);
	}

	@Override
	public List<Spot> findByCountry(String countryId, SpotType type, Page page, Activity activity) {
		return spotDao.querySpotsBySiteIdAndType(countryId, 3, type, page, activity);
	}

	@Override
	public List<Spot> findByProvince(String provinceId, SpotType type, Page page, Activity activity) {
		return spotDao.querySpotsBySiteIdAndType(provinceId, 5, type, page, activity);

	}

	@Override
	public List<Spot> findByCity(String cityId, SpotType type, Page page, Activity activity) {
		return spotDao.querySpotsBySiteIdAndType(cityId, 7, type, page, activity);
	}

}
